from selenium import webdriver
from hyrobot.common import *
from time import sleep
from lib.webui import *

# 移动到__st__.py
# def suite_setup():
#     INFO('suite_setup')
#     wb = open_broswer()
#     mgr_login(wb)
# def suite_teardown():
#     INFO('suite_teardown')
#     wb = get_global_webdriver()
#     wb.quit()

class c1:
    # 用例名字
    # run --test '管理员首页 - UI-0101'      这个命令就执行这个用例
    name = '管理员首页 - UI-0101'
    # 测试用例步骤

    def setup(self):
        # wb = open_broswer()
        # mgr_login(wb)

        INFO('c1_setup')

    def teardown(self):
        # wb = get_global_webdriver()
        # wb.quit()

        INFO('c1_teardown')
    def teststeps(self):
        STEP(1,'登录网站')
        # wb = webdriver.Chrome()
        # wb.implicitly_wait(5)
        # wb.get('http://127.0.0.1/mgr/sign.html')
        # wb.find_element_by_id('username').send_keys('byhy')
        # wb.find_element_by_id('password').send_keys('88888888')
        # wb.find_element_by_tag_name('button').click()

        # wb = open_broswer()
        # mgr_login(wb)

        wb = get_global_webdriver()
        STEP(2,'获取左侧菜单信息')
        sidebarMenu =wb.find_element_by_class_name('sidebar-menu')
        elements = sidebarMenu.find_elements_by_tag_name('span')
        menuTitles = []
        for ele in elements:
            INFO(ele.text)  # 显示到终端
            menuTitles.append(ele.text)
        STEP(3, '检查是否正确')
        CHECK_POINT('侧边栏是否正确',menuTitles[:3]==['客户','药品','订单'])
        # wb.quit()

    '''
        wb = webdriver.Chrome()
        wb.implicitly_wait(5)
        wb.get('http://127.0.0.1/mgr/sign.html')
        wb.find_element_by_id('username').send_keys('byhy')
        wb.find_element_by_id('password').send_keys('88888888')
        wb.find_element_by_tag_name('button').click()
        sidebarMenu =wb.find_elements_by_class_name('sidebar-menu')
        elements = sidebarMenu.find_element_by_tag_name('span')
        menuTitles = []
        for ele in elements:
            print(ele.text)
            menuTitles.append(ele.text)

        print('**检查点** 侧边栏菜单是否正确',end='')
        if menuTitles[:3] == ['客户','药品','订单']
            print('通过')
        else:
            print('不通过！！！')
            exit(1)
        wb.quit()
    '''
class c2:
    name = '管理员首页 - UI-0101'

    def setup(self):
        # wb = open_broswer()
        # mgr_login(wb)

        INFO('c2_setup')

    def teardown(self):
        # wb = get_global_webdriver()
        # wb.quit()

        INFO('c2_teardown')
    def teststeps(self):
        STEP(1, '登录网站')
        # wb = webdriver.Chrome()
        # wb.implicitly_wait(5)
        # wb.get('http://127.0.0.1/mgr/sign.html')
        # wb.find_element_by_id('username').send_keys('byhy')
        # wb.find_element_by_id('password').send_keys('88888888')
        # wb.find_element_by_tag_name('button').click()

        # wb = open_broswer()
        # mgr_login(wb)

        wb = get_global_webdriver()
        STEP(2, '找到客户按钮')
        sidebarMenu = wb.find_element_by_class_name('sidebar-menu')
        elements = sidebarMenu.find_elements_by_tag_name('span')
        elements[0].click()
        STEP(3, '添加客户')
        wb.find_element_by_class_name('glyphicon-plus').click()

        inputs = wb.find_element_by_class_name('add-one-area').find_elements_by_class_name('form-control')
        inputs[0].send_keys('南京中医院')
        inputs[1].send_keys('12345678911')
        inputs[2].send_keys('南京一路')
        wb.find_element_by_class_name('add-one-area').find_element_by_class_name('btn-xs').click()
        sleep(1)
        STEP(4,'检查添加信息')
        item = wb.find_elements_by_class_name('search-result-item')[0]
        fields = item.find_elements_by_tag_name('span')[:6]
        texts = [field.text for field in fields]
        print(texts)
        expected = [
            '客户名',
            '南京中医院',
            '联系电话',
            '12345678911',
            '地址',
            '南京一路',
        ]
        CHECK_POINT('客户信息和添加内容一致',
                    texts == expected)
        # wb.quit()